// pages/address/address.js
const poiListUrl = "https://restapi.amap.com/v3/place/text"
const area = require("../../utils/citys.js");
const location = require('../../utils/location.js')
const app = getApp()
Page({
    data: {
        provinceName: [],
        provinceCode: [],
        cityName: [],
        cityCode: [],
        districtName: [],
        districtCode: [],
        p: 0,
        c: 0,
        d: 0,
        showDistpicker: false
    },
    onLoad: function() {
        // 初始化数据
        location.getLocation(this).then(res => {
            return location.loadAddress(this)
        }).then(res => {
            this.setAreaData()
        })

    },
    setAreaData: function() {
        let {
            p = 0, c = 0, d = 0, dtCode = '', ctCode = '', pcCode = '', value = [0, 0, 0],
        } = this.data
        // 设置省的数据
        var province = area['100000']
        var provinceName = [];
        var provinceCode = [];
        for (var item in province) {
            provinceName.push(province[item])
            provinceCode.push(item)
        }
        if (pcCode) {
            p = provinceCode.indexOf(pcCode)
        }
        // 设置市的数据
        var city = area[provinceCode[p]]
        var cityName = [];
        var cityCode = [];
        for (var item in city) {
            cityName.push(city[item])
            cityCode.push(item)
        }
        if (ctCode) {
            c = cityCode.indexOf(ctCode)
        }
        // 设置区的数据
        var district = area[cityCode[c]]
        var districtName = [];
        var districtCode = [];
        for (var item in district) {
            districtName.push(district[item])
            districtCode.push(item)
        }
        if (dtCode) {
            d = districtCode.indexOf(dtCode)
        }
        if (p != -1 && c != -1 && d != -1) {
            value = [p, c, d]
            this.setData({
                p,
                c,
                d,
                value
            })
        }

        this.setData({
            provinceName,
            provinceCode,
            cityName,
            cityCode,
            districtName,
            districtCode
        })
    },
    changeArea: function(e) {
        console.log(e.detail.value)
        this.setData({
            value: e.detail.value,
            p: e.detail.value[0],
            c: e.detail.value[1],
            d: e.detail.value[2],
            dtCode: '',
            ctCode: '',
            pcCode: ''
        })
        this.setAreaData()
    },
    showDistpicker: function() {
        this.setData({
            showDistpicker: !this.data.showDistpicker
        })
    },
    /**
     *刷新
     */
    getUserLocation() {
        wx.removeStorage({ key: 'location', })
        location.getLocation(this).then(res => {
            return location.loadAddress(this)
        }).then(res => {
            this.setAreaData()
        })
    },
    /**
     * 搜索poi
     */
    search(event) {
        let keywords = event.detail.value
        app.toPromise(wx.request, {
            url: poiListUrl,
            data: {
                key: app.gdConfig.key,
                keywords: keywords,
                city: this.data.cityCode[this.data.c],
                offset: 8,
            }
        }).then(res => {
            this.setData({
                poiList: res.data.pois
            })
        })
    },
    /**
     * 选择好地址跳转
     */
    addressOk: function(evt) {
        let str = evt.currentTarget.dataset.location
        let lnglat = str.split(',')
        let index = evt.currentTarget.dataset.index
        let currentData = this.data.poiList[index]
        wx.setStorage({
            key: 'location',
            data: {
                lng: lnglat[0],
                lat: lnglat[1]
            },
        })
        wx.setStorage({
            key: 'address',
            data: currentData.pname + currentData.cityname + currentData.adname + currentData.name,
        })
        wx.navigateBack({
            delta: 1
        })
    }

})